package redis_cluster

import (
	"gamesdk/common/log"
	"time"

	"github.com/go-redis/redis"
)

type RedisCluster struct {
	addr    []string
	pass    string
	clients *redis.ClusterClient
}

// InitRedis 用于初始化redis
func InitRedisPass(ip []string, p string) (*RedisCluster, error) {
	redisCluster := new(RedisCluster)
	redisCluster.addr = ip
	redisCluster.pass = p
	return redisCluster, redisCluster.funcConnect()
}

func (this *RedisCluster) funcConnect() error {
	this.clients = redis.NewClusterClient(&redis.ClusterOptions{
		Addrs:    this.addr, //set redis cluster url
		Password: this.pass, //set password
	})
	_, err := this.clients.Ping().Result()
	if err != nil {
		log.Debug(err.Error())
		return err
	}
	log.Info("redis ip:%v,pass:%s", this.addr, this.pass)
	return nil
}

/*
*设置redis 储存键值对含有过期时间 这个函数被弃用,但是为了保证前面的代码能用,所以保留
*参数说明:
*@param:key		键
*@param:value	值
 */
func (this *RedisCluster) Set(key string, value interface{}) error {
	if this.clients == nil {
		//进行重连
		this.funcConnect()
	}
	err := this.clients.Set(key, value, 0).Err()
	return err
}

/*
*刷新当前数据库
*参数说明:
*		 无
 */
func (this *RedisCluster) FlushDB() error {
	if this.clients == nil {
		//进行重连
		this.funcConnect()
	}
	err := this.clients.FlushDB().Err()
	return err
}

/*
*返回与pattern匹配的字符串
*参数说明:
*@param:pattern	 模式字符串 e.g  "k*"
 */
func (this *RedisCluster) Keys(pattern string) ([]string, error) {
	if this.clients == nil {
		//进行重连
		this.funcConnect()
	}
	return this.clients.Keys(pattern).Result()
}

func (this *RedisCluster) DEL(Key string) (int, error) {
	if this.clients == nil {
		//进行重连
		this.funcConnect()
	}
	return 0, this.clients.Del(Key).Err()
}

/*
*清空所有数据库
*参数说明:
*@param:key		键
*@param:value	值
 */
func (this *RedisCluster) FlushAll() error {
	if this.clients == nil {
		//进行重连
		this.funcConnect()
	}
	return this.clients.FlushAll().Err()
}

/*
*测试redis是否能够连通
*参数说明:无
 */
func (this *RedisCluster) Ping() (string, error) {
	if this.clients == nil {
		//进行重连
		this.funcConnect()
	}
	return this.clients.Ping().Result()
}

/*
*设置redis 储存键值对含有过期时间 这个函数被弃用,但是为了保证前面的代码能用,所以保留
*参数说明:
*@param:key		键
*@param:value		值
*@param:tm		过期时间单位秒
 */
func (this *RedisCluster) SetByTime(key string, value interface{}, tm int) error {
	if this.clients == nil {
		//进行重连
		this.funcConnect()
	}
	return this.clients.Set(key, value, time.Second*time.Duration(tm)).Err()
}

/*
*设置redis 根据键获取值
*参数说明:
*@param:key		键
 */
func (this *RedisCluster) Get(key string) (string, error) {
	if this.clients == nil {
		//进行重连
		this.funcConnect()
	}
	return this.clients.Get(key).Result()
}

/*
* 如果不存在相关的key,value 则设置,否则不设置
* 参数说明:
* @param:key   redis中的key
* @param:value redis中的value
* @param:tm 	redis中的超时
 */
func (this *RedisCluster) SetNx(key string, value interface{}) (bool, error) {
	if this.clients == nil {
		//进行重连
		this.funcConnect()
	}
	return this.clients.SetNX(key, value, 0).Result()
}

func (this *RedisCluster) GETSET(key string, value interface{}) (string, error) {
	if this.clients == nil {
		//进行重连
		this.funcConnect()
	}
	return this.clients.GetSet(key, value).Result()
}
